package com.ahsjdy.st.stbocustomer.controller;

import com.ahsjdy.st.stservice.service.UserService;
import com.ahsjdy.stcommon.dto.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @Description
 * @Author xuetao wang
 * @Date 2020/2/3
 */
@RestController
public class CustomerController extends BaseController {

    @Autowired
    private CustomerServiceImpl customerService;


    @Autowired
    private UserService userService;

    @RequestMapping("/picture/{pictureName}")
    public void showPicture(HttpServletResponse response, @PathVariable("pictureName") String pictureName) {
        response.setContentType("image/png;charset=UTF-8");
        try (OutputStream os = response.getOutputStream();
             InputStream is = new FileInputStream(picturePath + pictureName)) {
            byte[] buf = new byte[2048];
            int length  = -1;
            while (-1 != (length = is.read(buf))) {
                os.write(buf, 0, length);
                os.flush();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @RequestMapping("/customer/listMerchant")
    public ResponseDTO listMerchant(int areaId) {
        return ResponseDTO.success(customerService.listMerchant(areaId));
    }

    @RequestMapping("/customer/myArea")
    public ResponseDTO listMerchant(String lon, String lat) {
        return ResponseDTO.success(customerService.choseMyArea(lon, lat));
    }

    @RequestMapping(value = "/customer/listAreas")
    public ResponseDTO listAreas() {
        return ResponseDTO.success(customerService.listAreas());
    }

    @RequestMapping(value = "/customer/loadMerchant")
    public ResponseDTO loadMerchant(int merchantId) {
        return ResponseDTO.success(customerService.loadMerchant(merchantId));
    }


    @RequestMapping(value = "/customer/listCommodity")
    public ResponseDTO listCommodity(int merchantId) {
        return ResponseDTO.success(customerService.listCommodity(merchantId));
    }

    /**
     * 微信登录接口
     * @return
     */
    @RequestMapping(value = "/user/wxLogin", method = RequestMethod.POST)
    public ResponseDTO wxLogin(String code) {
        return ResponseDTO.success(userService.wxLogin(code));
    }

}
